<template>
    <el-dialog ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="40%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="APP类型" prop="appType">
                <el-select v-model="ruleForm.appType" class="w100p" placeholder="请选择APP类型">
                    <el-option label="安卓" value="ANDROID"></el-option>
                    <el-option label="ios" value="IOS"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="更新内容" prop="updateContent">
                <el-input type="textarea" v-model="ruleForm.updateContent" placeholder="请输入更新内容"></el-input>
            </el-form-item>
            <el-form-item label="版本号" prop="version">
                <el-input  v-model="ruleForm.version" placeholder="请输入版本号"></el-input>
            </el-form-item>
            
            <el-form-item label="上传安装包">
                <el-upload class="upload" action="#" :file-list="fileList" :show-file-list="true" :limit="1"
                    :multiple="false" :auto-upload="false" :on-change="handleChange" :on-remove="handleRemove">
                    <el-button type="text">上传文件</el-button>
                </el-upload>
            </el-form-item>
            <el-form-item label="强制更新" prop="updates">
                <el-switch style="display: block" v-model="ruleForm.updates" active-color="#13ce66" inactive-color="#ff4949"
                    active-text="是" inactive-text="否">
                </el-switch>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>

import versionManage from "@/api/versionManage"
import files from "@/api/files"
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
    },
    data() {
        return {
            fileList: [],
            fileId: '',
            addEditName: '添加',
            formData: null,
            addEditApi: versionManage.add,
            ruleForm: {
                updates: false,
                appType: 'ANDROID'
            },
            rules: {
                version: [
                    { required: true, message: '请输入版本号', trigger: 'change' }
                ],
                appType: [
                    { required: true, message: '请选择App类型', trigger: 'change' }
                ],
                updateContent: [
                    { required: true, message: '请输入更新内容', trigger: 'change' }
                ],
                downloadUrl: [
                    { required: true, message: '请输入路径', trigger: 'change' }
                ],
            }
        }
    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    computed: {},
    mounted() {
    },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm();
                this.ruleForm = {
                    updates: false,
                    appType: 'ANDROID'
                }
                this.addEditApi = versionManage.add
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                let fileId = this.data.downloadUrl ? this.data.downloadUrl.split('fileId=')[1] : '';
                if (fileId) {
                    this.fileList = [{
                        name: this.data.downloadUrl,
                        url: this.data.downloadUrl
                    }]
                } else {
                    this.fileList = []
                }
                this.fileId = fileId
                this.ruleForm = { ...this.data };
                this.addEditApi = versionManage.edit
                this.addEditName = '编辑'
            }
        },
        handleRemove(file, fileList) {
            this.fileList = [];
            this.fileId = '';
            this.formData = null;
        },
        handleChange(file, fileList) {
            let formData = new FormData();
            formData.append("file", file.raw);
            formData.append('fileName', file.name);
            this.formData = formData;
            this.fileList = fileList;
        },
        async uploadFile(formData) {
            if (formData) {
                let res = await files.upload(formData);
                console.log(res)
                return res.data || '';
            } else {
                return ''
            }
        },
        getUrl(id) {
            let fileId = this.fileId || id;
            // let url = fileId ? `http://106.54.229.120:8081/firmwareService/versionManage/downloadApp?fileId=${fileId}` : '';
            // this.$set(this.ruleForm, 'downloadUrl', '');
            this.$set(this.ruleForm, 'fileId', fileId);
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate(async (valid) => {
                if (valid) {
                    let id = await this.uploadFile(this.formData);
                    this.getUrl(id);
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.msg)
                        this.cancel()
                    }).catch(err => {
                        this.$message.warning('操作失败')
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.fileList = [];
            this.formData = null;
            this.fileId = '';
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.line {
    border-bottom: 1px solid #e6e6e6;
}

.icon {
    font-size: 20px;
    line-height: 29px;
    cursor: pointer;
}

::v-deep .noLabel {
    .el-form-item {
        .label {
            display: none;
        }

        .el-form-item__content {
            margin: 0;
        }
    }
}
</style>
  